from rest_framework import serializers

from .models import Area


class AreaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Area
        fields = ('id', 'name')

# bj_area = Area.objects.get(id=1101000)
# bj_area.id
# bj_area.name
# bj_area.subs = [area_object, area_object, .....]


class SubAreaSerializer(serializers.ModelSerializer):
    # 这个序列化器的subs其实就是一个Area的对象集合，
    # 所以这里就用关联对象嵌套序列化
    subs = AreaSerializer(many=True, read_only=True)

    class Meta:
        model = Area
        fields = ('id', 'name', 'subs')